﻿<?php
session_start();
include "connectsql.php";

if (isset($_SESSION["ceo"]) && $_SESSION["ceo"] == true) {
	$manager = $_SESSION["manager"];
} else {
	die (ECHO_ERROR);	
}

if (isset($_REQUEST["file"])) {
	$file = $_REQUEST["file"];
} else {
	die (ECHO_ERROR);
}

if (isset($_REQUEST["extra"])) {
	$extra = $_REQUEST["extra"];
	if ($extra == "") {
		die (ECHO_ERROR);
	}
} else {
	die (ECHO_ERROR);
}

$path = "upload/".md5($manager.DB_PWD.$manager)."/";

$sql = "delete from ".SetDB_FILE_TAB($manager)." where `filename` = '".$file."' and `extrainfo` = '".$extra."'";
mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_DEL);

if ($extra == "1") {
	$pathname = "./".$path."tmp/".$file;
} else {
	$pathname = "./".$path.$file;
}

if (unlink($pathname)) {
	if ($extra == "0") {
		if (substr($file, -4) == ".xml") {
			do {
				if (substr($file, 0 , 4) == "mode") {
					$id = (int)(substr($file, 4, 3));
					$sql = "update ".SetDB_MODE_TAB($manager)." set `exist` = '0' where `id` = '".$id."'";
				}	else if (substr($file, 0 , 8) == "playlist") {
					$id = (int)(substr($file, 8, 3));
					$sql = "update ".SetDB_PLAYLIST_TAB($manager)." set `exist` = '0' where `id` = '".$id."'";
				} else {
					break;
				}	
				mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_CHANGE);
			} while (0);			
		}	else if (substr($file, -4) == ".zip") {
			$filenamelen = strlen($file);
			$filenamefolder = substr($file, 0, $filenamelen - 4);
			if (is_dir($path.$filenamefolder)) {
				remove_directory($path.$filenamefolder);
			}
			$sql = "delete from ".SetDB_FILE_TAB($manager)." where `filename` = '".$filenamefolder.".xml'";
			mysqli_query($link, $sql) or die(ECHO_MYSQL_ERROR_QUERY_TABLE_TO_DEL);			
		}
	}
	echo ECHO_OK;
} else {
	echo ECHO_ERROR;
}
mysqli_close($link);
?>